﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using System.Runtime.CompilerServices;
using Lamex.Base;
using Lamex.Interface;

namespace Lamex.mMachine.UI
{
    public partial class frmMAC_MT_Failure_Process : frmBaseChild
    {
        string strSql;
        DataTable dt;
        string IsEdit = "";
        public frmMAC_MT_Failure_Process()
        {
            InitializeComponent();
        }

        private void frmMAC_MT_Failure_Process_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
            gridView1.OptionsBehavior.Editable = false;
            //文字左对齐，直接在设计器中设置
            //vGridControl1.Appearance.RecordValue.HAlignment = DevExpress.Utils.HorzAlignment.Near;
            setvGridControlRepository();
            //维修编号，DMTyyyyMMddhhmmss
            Refreshes();
        }

        private void Refreshes()
        {
            strSql = @"SELECT MAINTAIN_NO,
c.WORKER_NAME as REPORT_PERSON,
REPORT_TIME,
a.MACHINE_NO,
b.MACHINE_DESC,
REPORT_STATE,
FAILURE_TYPE,
FAILURE_DESC,
RESPONSE_PERSON,
ACTUAL_STATE,
PROCESS_PLAN,
PROCESS_PERSON,
CONFIRM_PERSON,
a.PROCESS_TIME,
a.RESPONSE_TIME
FROM [LamexMES_FAMA].[dbo].[MT_Maintain_Record]  a left join
[LamexMESManager].[dbo].[FAMA_Account_Basic_Info]  b on a.MACHINE_NO=b.MACHINE_NO  left join
[LamexMES_FAMA].[dbo].[MT_Resource_Worker_List] c on a.REPORT_PERSON=c.WORKER_NO
WHERE  ISNULL(CONFIRM_PERSON,'')='' and a.UPDATE_USER='" + Lamex.Common.Loginer.CurrentUser.Account + "' " +
"ORDER BY a.REPORT_TIME desc";

            gridControl1.DataSource = null;
            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            if (dt.Rows.Count > 0)
            {
                gridControl1.DataSource = dt;
            }

            //遍历并设置自动列宽
            setGridViewColumnAutoWidth();

            resetGridViewEditProperties();
        }

        //设置控件可选项
        private void setvGridControlRepository()
        {
            vGridControl1.OptionsBehavior.Editable = false;
            vGridControl1.DataSource = null;
            //获取维修人信息
            DataTable dtMan = Common.GetDataTable.GetTable_RepairMan();
            //获取设备编号信息
            DataTable dtMachine = Common.GetDataTable.GetTable_Machine();

            ////REPORT_PERSON
            //DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit lookup_report = new DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit();
            //lookup_report.NullText = string.Empty;
            //if (dt.Select("REPORT_FLAG=1").Length > 0)
            //{
            //    DataTable dt_ReportPerson = dt.Select("REPORT_FLAG=1").CopyToDataTable().DefaultView.ToTable(true, new string[] { "WORKER_NO", "WORKER_NAME" });
            //    lookup_report.DataSource = dt_ReportPerson;
            //    lookup_report.ValueMember = "WORKER_NAME";
            //    lookup_report.DisplayMember = "WORKER_NAME";
            //}
            //RESPONSE_PERSON
            DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit lookup_response = new DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit();
            lookup_response.NullText = string.Empty;
            if (dtMan.Select("MAINTAIN_FLAG=1").Length > 0)
            {
                DataTable dt_ResponsePerson = dtMan.Select("MAINTAIN_FLAG=1").CopyToDataTable().DefaultView.ToTable(true, new string[] { "WORKER_NO", "WORKER_NAME" });
                lookup_response.DataSource = dt_ResponsePerson;
                lookup_response.ValueMember = "WORKER_NO";
                lookup_response.DisplayMember = "WORKER_NO";
            }
            ////CONFIRM_PERSON
            //DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit lookup_confirm = new DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit();
            //lookup_confirm.NullText = string.Empty;
            //if (dt.Select("CONFIRM_FLAG=1").Length > 0)
            //{
            //    DataTable dt_ConfirmPerson = dt.Select("CONFIRM_FLAG=1").CopyToDataTable().DefaultView.ToTable(true, new string[] { "WORKER_NO", "WORKER_NAME" });
            //    lookup_confirm.DataSource = dt_ConfirmPerson;
            //    lookup_confirm.ValueMember = "WORKER_NAME";
            //    lookup_confirm.DisplayMember = "WORKER_NAME";
            //}
            //MACHINE_NO
            DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit lookup_machine = new DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit();
            lookup_machine.NullText = string.Empty;
            if (dtMachine.Rows.Count > 0)
            {
                lookup_machine.DataSource = dtMachine;
                lookup_machine.ValueMember = "MACHINE_NO";
                lookup_machine.DisplayMember = "MACHINE_NO";
            }

           // vGridControl1.Rows["rowREPORT_PERSON"].Properties.RowEdit = lookup_report;
            ////加载SELECT [WORKER_NO],[WORKER_NAME] FROM [LamexMES_FAMA].[dbo].[MT_Resource_Worker_List] WHERE REPORT_FLAG=1
            //vGridControl1.Rows["rowRESPONSE_PERSON"].Properties.RowEdit = lookup_response;
            //加载SELECT [WORKER_NO],[WORKER_NAME] FROM [LamexMES_FAMA].[dbo].[MT_Resource_Worker_List] WHERE MAINTAIN_FLAG=1
            vGridControl1.Rows["rowPROCESS_PERSON"].Properties.RowEdit = lookup_response;
            //加载SELECT [WORKER_NO],[WORKER_NAME] FROM [LamexMES_FAMA].[dbo].[MT_Resource_Worker_List] WHERE MAINTAIN_FLAG=1
            //vGridControl1.Rows["rowCONFIRM_PERSON"].Properties.RowEdit = lookup_confirm;
            //加载SELECT [WORKER_NO],[WORKER_NAME] FROM [LamexMES_FAMA].[dbo].[MT_Resource_Worker_List] WHERE CONFIRM_FLAG=1

            vGridControl1.Rows["rowMACHINE_NO"].Properties.RowEdit = lookup_machine;
            //加载SELECT[MACHINE_NO],[MACHINE_DESC] FROM[LamexMESManager].[dbo].[FAMA_Account_Basic_Info]

            //STATE
            DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboBox_state = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            comboBox_state.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            string[] reportList = new string[] { "已停机", "未停机" };
            foreach (var item in reportList)
            {
                comboBox_state.Items.Add(item);
            }
            //PROCESS_PLAN
            DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboBox_type = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            comboBox_type.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            string[] typeList = new string[] { "内部维修", "委外维修", "定期保养", "非设备问题", "无维修价值", "误报" };
            foreach (var item in typeList)
            {
                comboBox_type.Items.Add(item);
            }
            //FAILURE_TYPE
            DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboBox_failType = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            comboBox_failType.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            string[] failList = Common.GetDataTable.GetArray_Type().Split(',');
            foreach (var item in failList)
            {
                comboBox_failType.Items.Add(item);
            }

            vGridControl1.Rows["rowREPORT_STATE"].Properties.RowEdit = comboBox_state;
            //加载选项“已停机,未停机"，不允许用户后期增加选项，所以静态加载
            vGridControl1.Rows["rowACTUAL_STATE"].Properties.RowEdit = comboBox_state;
            //加载选项“已停机,未停机"
            vGridControl1.Rows["rowPROCESS_PLAN"].Properties.RowEdit = comboBox_type;
            //加载选项“内部维修,委外维修,定期保养,非设备问题,无维修价值,误报”

            vGridControl1.Rows["rowFAILURE_TYPE"].Properties.RowEdit = comboBox_failType;
            //加载表[Sys_ControlValueOption].[FIELD_NAME]=FAMA_MT_FAILURE_TYPE，即“机械,电气,皮带,网络,其他”拆分结果
            //因允许用户后期增加选项，所以动态加载
            //SELECT[FIELD_VALUE_OPTION] FROM[LamexMESManager].[dbo].[Sys_ControlValueOption] WHERE FIELD_NAME = 'FAMA_MT_FAILURE_TYPE'

            DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit memoEdit = new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit();
            vGridControl1.Rows["rowFAILURE_DESC"].Properties.RowEdit = memoEdit;

            this.repositoryItemTextEdit1.Enter += RepositoryItemTextEdit1_Enter;
        }     

        private void frmMAC_MT_Failure_Process_Resize(object sender, EventArgs e)
        {
            splitContainerControl1.SplitterPosition = this.Width - 330;
        }

        private void setGridViewColumnAutoWidth()
        {
            gridView1.OptionsView.ColumnAutoWidth = false;
            for (int i = 0; i < gridView1.Columns.Count; i++)
            {
                gridView1.BestFitColumns();
                if (i == 2)
                {
                    gridView1.Columns[2].Width = 20;
                }
                else
                {
                    gridView1.Columns[i].BestFit();//自动列宽    
                }
            }
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            resetGridViewEditProperties();
        }

        private void resetGridViewEditProperties()
        {
            //vGridControl1.OptionsBehavior.Editable = true;//已在设计器中设置

            rowMAINTAIN_NO.Properties.AllowEdit = false; rowMAINTAIN_NO.Enabled = false;
            rowREPORT_TIME.Properties.AllowEdit = false; rowREPORT_TIME.Enabled = false;
            rowRESPONSE_TIME.Properties.AllowEdit = false; rowRESPONSE_TIME.Enabled = false;
            rowPROCESS_TIME.Properties.AllowEdit = false; rowPROCESS_TIME.Enabled = false;
            rowCONFIRM_TIME.Properties.AllowEdit = false; rowCONFIRM_TIME.Enabled = false;

            if (radioGroup1.SelectedIndex == 0) //故障上报
            {
                rowREPORT_PERSON.Properties.AllowEdit = true; rowREPORT_PERSON.Enabled = true;
                rowMACHINE_NO.Properties.AllowEdit = true; rowMACHINE_NO.Enabled = true;
                rowREPORT_STATE.Properties.AllowEdit = true; rowREPORT_STATE.Enabled = true;
                rowFAILURE_TYPE.Properties.AllowEdit = true; rowFAILURE_TYPE.Enabled = true;
                rowFAILURE_DESC.Properties.AllowEdit = true; rowFAILURE_DESC.Enabled = true;

                rowRESPONSE_PERSON.Properties.AllowEdit = false; rowRESPONSE_PERSON.Enabled = false;
                rowACTUAL_STATE.Properties.AllowEdit = false; rowACTUAL_STATE.Enabled = false;
                rowPROCESS_PLAN.Properties.AllowEdit = false; rowPROCESS_PLAN.Enabled = false;
                rowPROCESS_PERSON.Properties.AllowEdit = false; rowPROCESS_PERSON.Enabled = false;

                rowCONFIRM_PERSON.Properties.AllowEdit = false; rowCONFIRM_PERSON.Enabled = false;
            }
            else if (radioGroup1.SelectedIndex == 1)    //故障响应
            {
                rowREPORT_PERSON.Properties.AllowEdit = false; rowREPORT_PERSON.Enabled = false;
                rowMACHINE_NO.Properties.AllowEdit = false; rowMACHINE_NO.Enabled = false;
                rowREPORT_STATE.Properties.AllowEdit = false; rowREPORT_STATE.Enabled = false;
                rowFAILURE_TYPE.Properties.AllowEdit = false; rowFAILURE_TYPE.Enabled = false;
                rowFAILURE_DESC.Properties.AllowEdit = false; rowFAILURE_DESC.Enabled = false;

                rowRESPONSE_PERSON.Properties.AllowEdit = true; rowRESPONSE_PERSON.Enabled = true;
                rowACTUAL_STATE.Properties.AllowEdit = true; rowACTUAL_STATE.Enabled = true;
                rowPROCESS_PLAN.Properties.AllowEdit = true; rowPROCESS_PLAN.Enabled = true;
                rowPROCESS_PERSON.Properties.AllowEdit = true; rowPROCESS_PERSON.Enabled = true;

                rowCONFIRM_PERSON.Properties.AllowEdit = false; rowCONFIRM_PERSON.Enabled = false;
            }
            else if (radioGroup1.SelectedIndex == 2)    //维修确认
            {
                rowREPORT_PERSON.Properties.AllowEdit = false; rowREPORT_PERSON.Enabled = false;
                rowMACHINE_NO.Properties.AllowEdit = false; rowMACHINE_NO.Enabled = false;
                rowREPORT_STATE.Properties.AllowEdit = false; rowREPORT_STATE.Enabled = false;
                rowFAILURE_TYPE.Properties.AllowEdit = false; rowFAILURE_TYPE.Enabled = false;
                rowFAILURE_DESC.Properties.AllowEdit = false; rowFAILURE_DESC.Enabled = false;

                rowRESPONSE_PERSON.Properties.AllowEdit = false; rowRESPONSE_PERSON.Enabled = false;
                rowACTUAL_STATE.Properties.AllowEdit = false; rowACTUAL_STATE.Enabled = false;
                rowPROCESS_PLAN.Properties.AllowEdit = false; rowPROCESS_PLAN.Enabled = false;
                rowPROCESS_PERSON.Properties.AllowEdit = false; rowPROCESS_PERSON.Enabled = false;

                rowCONFIRM_PERSON.Properties.AllowEdit = true; rowCONFIRM_PERSON.Enabled = true;
            }
        }

        private void sbtnSave_Click(object sender, EventArgs e)
        {
            try
            {

                string strREPORT_PERSON = vGridControl1.Rows["rowREPORT_PERSON"].Properties.Value!=null?
                    vGridControl1.Rows["rowREPORT_PERSON"].Properties.Value.ToString():"";//报修人
                string dtmREPORT_TIME = DateTime.Now.ToString();//报修时间
                string strMACHINE_NO = vGridControl1.Rows["rowMACHINE_NO"].Properties.Value!=null?
                    vGridControl1.Rows["rowMACHINE_NO"].Properties.Value.ToString():"";//设备编号
                string strREPORT_STATE = vGridControl1.Rows["rowREPORT_STATE"].Properties.Value!=null?
                    vGridControl1.Rows["rowREPORT_STATE"].Properties.Value.ToString():"";//报修时设备状态
                string strFAILURE_TYPE = vGridControl1.Rows["rowFAILURE_TYPE"].Properties.Value!=null?
                    vGridControl1.Rows["rowFAILURE_TYPE"].Properties.Value.ToString():"";//故障类别
                string strFAILURE_DESC = vGridControl1.Rows["rowFAILURE_DESC"].Properties.Value!=null?
                    vGridControl1.Rows["rowFAILURE_DESC"].Properties.Value.ToString():"";//故障描述
                string strRESPONSE_PERSON = vGridControl1.Rows["rowRESPONSE_PERSON"].Properties.Value != null ?
                    vGridControl1.Rows["rowRESPONSE_PERSON"].Properties.Value.ToString().ToUpper() : "";//响应人
                string strACTUAL_STATE = vGridControl1.Rows["rowACTUAL_STATE"].Properties.Value != null ?
                      vGridControl1.Rows["rowACTUAL_STATE"].Properties.Value.ToString() : "";//实际状态
                string strPROCESS_PLAN = vGridControl1.Rows["rowPROCESS_PLAN"].Properties.Value != null ?
                    vGridControl1.Rows["rowPROCESS_PLAN"].Properties.Value.ToString() : "";//维修方案
                string strPROCESS_PERSON = vGridControl1.Rows["rowPROCESS_PERSON"].Properties.Value != null ?
                    vGridControl1.Rows["rowPROCESS_PERSON"].Properties.Value.ToString().ToUpper() : "";//处理人
                string strUPDATE_TIME = DateTime.Now.ToString();
                string strUPDATE_USERID = Lamex.Common.Loginer.CurrentUser.Account;
                if (string.IsNullOrEmpty(strREPORT_PERSON) || string.IsNullOrEmpty(strMACHINE_NO) || string.IsNullOrEmpty(strREPORT_STATE)
                    || string.IsNullOrEmpty(strFAILURE_TYPE) || string.IsNullOrEmpty(strFAILURE_DESC))
                {
                    XtraMessageBox.Show("必填项为空,不允许保存！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (IsEdit == "Add")
                {
                    DataTable table_Report = Common.GetDataTable.GetTable_ReportMan_WorkerNo(strREPORT_PERSON);
                    if (table_Report.Rows.Count <= 0)
                    {
                        XtraMessageBox.Show("该报修人没有报修权限！请联系设备管理员开通！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    //保存故障上报信息
                    string strMAINTAIN_NO = "DMT" + DateTime.Now.ToString("yyyyMMddHHmmss");//维修编号              
                    if (Common.CRUDHelper.Insert_MT_Maintain_Record(strMAINTAIN_NO, strREPORT_PERSON, dtmREPORT_TIME, strMACHINE_NO, strREPORT_STATE, strFAILURE_TYPE, strFAILURE_DESC,strUPDATE_TIME,strUPDATE_USERID))
                    {
                        using (DevExpress.Utils.WaitDialogForm wdf = new DevExpress.Utils.WaitDialogForm("请稍等", "正在发送邮件......", new Size(400, 80)))
                        {
                            DataTable table = Common.CRUDHelper.Get_FAMA_MachineInfo(strMACHINE_NO);
                            string strMachine_Desc = table.Rows[0]["MACHINE_DESC"].ToString();
                            string strMACHINE_NAME= table.Rows[0]["MACHINE_NAME"].ToString();
                            string strMACHINE_LOCATION= table.Rows[0]["MACHINE_LOCATION"].ToString();
                            string strMACHINE_USING_DEPT= table.Rows[0]["MACHINE_USING_DEPT"].ToString();
                            string strPHONE= table.Rows[0]["PHONE_NO"].ToString();
                            strREPORT_PERSON = table_Report.Rows[0]["WORKER_NAME"].ToString();//报修人转换成姓名发邮件
                            string[] Addresslist = null;
                            DataTable table_mail = Common.CRUDHelper.Get_SendMail(strMACHINE_USING_DEPT);
                            if (table_mail.Rows.Count > 0)
                            {
                                if (table_mail.Rows[0]["LEVEL1_RECEIVER"].ToString().Length > 0)
                                    Addresslist = table_mail.Rows[0]["LEVEL1_RECEIVER"].ToString().Split(';');
                            }
                            Common.MailHelper.SendMail(Addresslist, strMAINTAIN_NO, strREPORT_PERSON, dtmREPORT_TIME, strMACHINE_NO,
                                strMachine_Desc, strREPORT_STATE, strFAILURE_TYPE, strFAILURE_DESC, strMACHINE_NAME, strMACHINE_LOCATION, strPHONE);
                            //刷新页面
                            Refreshes();
                            XtraMessageBox.Show("故障上报新增成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
                //更新故障上报信息
                if (IsEdit == "Edit" && radioGroup1.SelectedIndex == 0)
                {
                    XtraMessageBox.Show("故障上报后不允许修改！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    // string strMAINTAIN_NO = vGridControl1.Rows["rowMAINTAIN_NO"].Properties.Value.ToString();
                    //if (Common.CRUDHelper.Update_MT_Maintain_Record(strMAINTAIN_NO, strREPORT_PERSON, dtmREPORT_TIME, strMACHINE_NO, strREPORT_STATE, strFAILURE_TYPE, strFAILURE_DESC))
                    //{
                    //    //刷新页面
                    //    Refresh();
                    //    XtraMessageBox.Show("故障上报修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //}
                }
                //更新故障响应信息
                if (IsEdit == "Edit" && radioGroup1.SelectedIndex == 1)
                {
                    string dtmPROCESS_TIME = "NULL";
                    string dtmRESPONSE_TIME = DateTime.Now.ToString();//响应时间
                    string strMAINTAIN_NO = vGridControl1.Rows["rowMAINTAIN_NO"].Properties.Value.ToString();
                    
                    if (string.IsNullOrEmpty(strRESPONSE_PERSON))
                    {
                        XtraMessageBox.Show("响应人不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    //响应人如存在,响应时间取数据库
                    DataTable table = Common.CRUDHelper.Get_MT_Maintain_Record(strMAINTAIN_NO);
                    if (table.Rows.Count > 0)
                    {
                        string tempRESPONSE_PERSON = table.Rows[0]["RESPONSE_PERSON"].ToString();
                        if (!string.IsNullOrEmpty(tempRESPONSE_PERSON))
                        {
                            dtmRESPONSE_TIME = table.Rows[0]["RESPONSE_TIME"].ToString();
                        }
                    }

                    //判断响应人是否有维修权限
                    DataTable table_Response = Common.GetDataTable.GetTable_RepairMan_WorkerNo(strRESPONSE_PERSON);
                    if(table_Response.Rows.Count<=0)
                    {
                        XtraMessageBox.Show("该响应人没有维修权限！请联系设备管理员开通！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    if (!string.IsNullOrEmpty(strPROCESS_PERSON))
                        dtmPROCESS_TIME = "'" + DateTime.Now.ToString() + "'"; //处理时间

                    if (Common.CRUDHelper.UPdate_MT_Maintain_Respone(strMAINTAIN_NO, strRESPONSE_PERSON, dtmRESPONSE_TIME, strACTUAL_STATE, strPROCESS_PLAN, strPROCESS_PERSON, dtmPROCESS_TIME))
                    {
                        //委外维修时插一条记录到MT_Outsource_Record
                        if (strPROCESS_PLAN.Contains("委外"))
                        {
                            string strOUTSOURCE_NO = "DOS" + DateTime.Now.ToString("yyyyMMddHHmmss");//委外编号 
                            string strUPDATE_TS = DateTime.Now.ToString();
                            string strUPDATE_USER = Lamex.Common.Loginer.CurrentUser.Account;
                            if (Common.CRUDHelper.Insert_MT_Outsource_Record(strOUTSOURCE_NO, strMAINTAIN_NO, strUPDATE_TS, strUPDATE_USER))
                            {
                                //插入委外记录成功
                            }
                        }
                        //刷新页面
                        Refreshes();
                        XtraMessageBox.Show("故障响应修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                //故障完成确认
                if (IsEdit == "Edit" && radioGroup1.SelectedIndex == 2)
                {
                    string strMAINTAIN_NO = vGridControl1.Rows["rowMAINTAIN_NO"].Properties.Value.ToString();
                    string strCONFIRM_PERSON = vGridControl1.Rows["rowCONFIRM_PERSON"].Properties.Value!=null?
                        vGridControl1.Rows["rowCONFIRM_PERSON"].Properties.Value.ToString().ToUpper():"";//完成人
                    string dtmCONFIRM_TIME = DateTime.Now.ToString();//完成时间
                    if(string.IsNullOrEmpty(strRESPONSE_PERSON))
                    {
                        XtraMessageBox.Show("响应人不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    if (string.IsNullOrEmpty(strACTUAL_STATE))
                    {
                        XtraMessageBox.Show("实际状态不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    if (string.IsNullOrEmpty(strPROCESS_PLAN))
                    {
                        XtraMessageBox.Show("维修方案不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return; 
                    }
                    if (string.IsNullOrEmpty(strPROCESS_PERSON))
                    {
                        XtraMessageBox.Show("处理人不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return; 
                    }
                    if (string.IsNullOrEmpty(strCONFIRM_PERSON))
                    {
                        XtraMessageBox.Show("完成人不允许为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    //判断处理人是否有维修权限
                    DataTable table_Confirm = Common.GetDataTable.GetTable_ConfirmMan_WorkerNo(strCONFIRM_PERSON);
                    if (table_Confirm.Rows.Count <= 0)
                    {
                        XtraMessageBox.Show("该完成确认人没有确认权限！请联系设备管理员开通！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    //委外维修时必须先确认委外表[ISSUE_STATE]是否有值,没有值不能保存
                    if (strPROCESS_PLAN.Contains("委外"))
                    {
                        DataTable table_Outsource = Common.CRUDHelper.Get_MT_Outsource_Record(strMAINTAIN_NO);
                        if (table_Outsource.Rows.Count > 0)
                        {
                            string strISSUE_STATE = table_Outsource.Rows[0]["ISSUE_STATE"].ToString();
                            if (string.IsNullOrEmpty(strISSUE_STATE))
                            {
                                XtraMessageBox.Show("该故障需要委外维修,请先完成委外维修后再确认！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                return;
                            }
                        }
                    }
                    if (Common.CRUDHelper.UPdate_MT_Maintain_CONFIRM(strMAINTAIN_NO, strCONFIRM_PERSON, dtmCONFIRM_TIME))
                    {
                        //刷新页面
                        Refreshes();
                        XtraMessageBox.Show("故障完成确认！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                //每次保存后锁定信息输入
                vGridControl1.OptionsBehavior.Editable = false;
                //vGridControl1.DataSource = null;
                for (int i = 0; i < vGridControl1.Rows.Count; i++)
                {
                    vGridControl1.SetCellValue(vGridControl1.Rows[i], 0, null);
                }

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
        public override void DoAdd(Lamex.Interface.IButtonInfo sender)
        {
            //选项为故障上报时才可以新增
            if (radioGroup1.SelectedIndex == 0)
            {
                vGridControl1.OptionsBehavior.Editable = true;
                rowREPORT_PERSON.Properties.AllowEdit = true; rowREPORT_PERSON.Enabled = true;
                rowMACHINE_NO.Properties.AllowEdit = true; rowMACHINE_NO.Enabled = true;
                rowREPORT_STATE.Properties.AllowEdit = true; rowREPORT_STATE.Enabled = true;
                rowFAILURE_TYPE.Properties.AllowEdit = true; rowFAILURE_TYPE.Enabled = true;
                rowFAILURE_DESC.Properties.AllowEdit = true; rowFAILURE_DESC.Enabled = true;
                if (vGridControl1.Rows.Count > 0)
                    for (int i = 0; i < vGridControl1.Rows.Count; i++)
                    {
                        vGridControl1.SetCellValue(vGridControl1.Rows[i], 0, null);
                    }
                IsEdit = "Add";
                //焦点到报修人
                vGridControl1.FocusedRow = vGridControl1.Rows["rowREPORT_PERSON"];
            }
        }
        public override void DoEdit(IButtonInfo sender)
        {
            SetVGridControl();
        }
        public override void DoView(Lamex.Interface.IButtonInfo sender)
        {
            Refreshes();
        }
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            SetVGridControl();
        }
        private void SetVGridControl()
        {
            vGridControl1.OptionsBehavior.Editable = true;
            IsEdit = "Edit";
            //获取当前行的数据
            int iRow = gridView1.FocusedRowHandle;
            string strMAINTAIN_NO = gridView1.GetRowCellDisplayText(iRow, "MAINTAIN_NO").ToString();//维修编号
            string strREPORT_PERSON = gridView1.GetRowCellDisplayText(iRow, "REPORT_PERSON").ToString();//报修人
            string strREPORT_TIME = gridView1.GetRowCellDisplayText(iRow, "REPORT_TIME").ToString();//报修时间
            string strMACHINE_NO = gridView1.GetRowCellDisplayText(iRow, "MACHINE_NO").ToString();//设备编号
            string strREPORT_STATE = gridView1.GetRowCellDisplayText(iRow, "REPORT_STATE").ToString();//报修时设备状态
            string strFAILURE_TYPE = gridView1.GetRowCellDisplayText(iRow, "FAILURE_TYPE").ToString();//故障类别
            string strFAILURE_DESC = gridView1.GetRowCellDisplayText(iRow, "FAILURE_DESC").ToString();//故障描述
            string strRESPONSE_PERSON = gridView1.GetRowCellDisplayText(iRow, "RESPONSE_PERSON").ToString();//响应人
            string strRESPONSE_TIME = gridView1.GetRowCellDisplayText(iRow, "RESPONSE_TIME").ToString();//响应时间
            string strACTUAL_STATE = gridView1.GetRowCellDisplayText(iRow, "ACTUAL_STATE").ToString();//实际状态
            string strPROCESS_PLAN = gridView1.GetRowCellDisplayText(iRow, "PROCESS_PLAN").ToString();//处理方案
            string strPROCESS_PERSON = gridView1.GetRowCellDisplayText(iRow, "PROCESS_PERSON").ToString();//处理人
            string strPROCESS_TIME = gridView1.GetRowCellDisplayText(iRow, "PROCESS_TIME").ToString();//处理时间

            //把数据显示到右边的VGridControl
            vGridControl1.Rows["rowMAINTAIN_NO"].Properties.Value = strMAINTAIN_NO;
            vGridControl1.Rows["rowREPORT_PERSON"].Properties.Value = strREPORT_PERSON;
            vGridControl1.Rows["rowREPORT_TIME"].Properties.Value = strREPORT_TIME;
            vGridControl1.Rows["rowMACHINE_NO"].Properties.Value = strMACHINE_NO;
            vGridControl1.Rows["rowREPORT_STATE"].Properties.Value = strREPORT_STATE;
            vGridControl1.Rows["rowFAILURE_TYPE"].Properties.Value = strFAILURE_TYPE;
            vGridControl1.Rows["rowFAILURE_DESC"].Properties.Value = strFAILURE_DESC;

            vGridControl1.Rows["rowRESPONSE_PERSON"].Properties.Value = strRESPONSE_PERSON;
            vGridControl1.Rows["rowRESPONSE_TIME"].Properties.Value = strRESPONSE_TIME;
            vGridControl1.Rows["rowACTUAL_STATE"].Properties.Value = strACTUAL_STATE;
            vGridControl1.Rows["rowPROCESS_PLAN"].Properties.Value = strPROCESS_PLAN;
            vGridControl1.Rows["rowPROCESS_PERSON"].Properties.Value = strPROCESS_PERSON;
            vGridControl1.Rows["rowPROCESS_TIME"].Properties.Value = strPROCESS_TIME;
            if(radioGroup1.SelectedIndex==0)
            {
                rowREPORT_PERSON.Properties.AllowEdit = false; rowREPORT_PERSON.Enabled = false;
                rowMACHINE_NO.Properties.AllowEdit = false; rowMACHINE_NO.Enabled = false;
                rowREPORT_STATE.Properties.AllowEdit = false; rowREPORT_STATE.Enabled = false;
                rowFAILURE_TYPE.Properties.AllowEdit = false; rowFAILURE_TYPE.Enabled = false;
                rowFAILURE_DESC.Properties.AllowEdit = false; rowFAILURE_DESC.Enabled = false;
                vGridControl1.FocusedRow = vGridControl1.Rows["rowREPORT_PERSON"];
            }
            //响应人和响应时间保存后不允许修改
            if (radioGroup1.SelectedIndex == 1)
            {
                rowRESPONSE_PERSON.Properties.AllowEdit = true;
                rowRESPONSE_PERSON.Enabled = true;
                if (!string.IsNullOrEmpty(strRESPONSE_PERSON))
                {
                    DataTable table = Common.CRUDHelper.Get_MT_Maintain_Record(strMAINTAIN_NO);
                    if (table.Rows.Count > 0)
                    {
                        string tempRESPONSE_PERSON = table.Rows[0]["RESPONSE_PERSON"].ToString();
                        if (!string.IsNullOrEmpty(tempRESPONSE_PERSON))
                        {
                            rowRESPONSE_PERSON.Properties.AllowEdit = false;
                            rowRESPONSE_PERSON.Enabled = false;
                        }
                    }
                }
                vGridControl1.FocusedRow = vGridControl1.Rows["rowRESPONSE_PERSON"];
            }
            if(radioGroup1.SelectedIndex==2)
            {
                vGridControl1.FocusedRow = vGridControl1.Rows["rowCONFIRM_PERSON"];
            }
        }
        #region 禁用输入法
        TextBoxMaskBox GetInnerEdit(BaseEdit editor)
        {
            foreach (Control innerEditor in editor.Controls)
                if (innerEditor is TextBoxMaskBox)
                    return innerEditor as TextBoxMaskBox;
            return null;
        }
        private void RepositoryItemTextEdit1_Enter(object sender, EventArgs e)
        {
            TextBoxMaskBox innerEditor = GetInnerEdit(sender as TextEdit);
            if (innerEditor != null)
                innerEditor.ImeMode = ImeMode.Disable;
        }
        #endregion

        private void sbtnView_Click(object sender, EventArgs e)
        {
            DoView(null);
        }
    }
}